package com.goods.controller.business;


import com.goods.common.model.business.InStock;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.Supplier;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.InStockDetailVO;
import com.goods.common.vo.business.InStockItemVO;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.business.SupplierVO;
import com.goods.service.InStockService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/business/inStock")
public class InStockController {

    @Autowired
    InStockService inStockService;

    @GetMapping("/findInStockList")
    public ResponseBean<Map<Object,Object>> findInStockList(
            Integer pageNum, Integer pageSize, Integer status,
            Integer type, String inNum, String startTime, String endTime
            ){
        Map map = new HashMap();
        List<InStock> inStockList = null;
        if (status == 0 && StringUtils.isEmpty(type) && StringUtils.isEmpty(inNum) && StringUtils.isEmpty(startTime) && StringUtils.isEmpty(endTime)){
            inStockList = inStockService.findInStockList(status);
        }else {
            inStockList = inStockService.findInStockList(status,type,inNum,startTime,endTime);
        }
        List<InStockVO> collect = inStockList.stream().map(inStock -> {
            InStockVO inStockVO = new InStockVO();
            Supplier supplier = inStockService.findSupplier(inStock.getSupplierId());
            BeanUtils.copyProperties(inStock,inStockVO);
            inStockVO.setPhone(supplier.getPhone());
            inStockVO.setSupplierName(supplier.getName());
            return inStockVO;
        }).collect(Collectors.toList());
        List<InStockVO> pageList = ListPageUtils.page(collect, pageSize, pageNum);
        map.put("total",collect.size());
        map.put("rows",pageList);
        return ResponseBean.success(map);
    }

    @GetMapping("/detail/{id}")
    public ResponseBean detail(@PathVariable Long id,Integer pageNum){
        List<InStockItemVO> list = inStockService.detail(id,pageNum);
        InStock inStock = inStockService.selectInStock(id);
        SupplierVO supplierVO = inStockService.selectSupplierVO(inStock.getSupplierId());
        InStockDetailVO inStockDetailVO = new InStockDetailVO();
        inStockDetailVO.setInNum(inStock.getInNum());
        inStockDetailVO.setStatus(inStock.getStatus());
        inStockDetailVO.setType(inStock.getType());
        inStockDetailVO.setOperator(inStock.getOperator());
        inStockDetailVO.setSupplierVO(supplierVO);
        inStockDetailVO.setTotal(list.size());
        inStockDetailVO.setItemVOS(list);
        return ResponseBean.success(inStockDetailVO);
    }

    @PutMapping("/remove/{id}")
    public ResponseBean remove(@PathVariable Long id){
        int i = inStockService.remove(id);
        if (i == 0){
            return ResponseBean.error("移入回收站失败");
        }else {
            return ResponseBean.success();
        }
    }

    @PutMapping("/back/{id}")
    public ResponseBean back(@PathVariable Long id){
        int i = inStockService.back(id);
        if (i == 0){
            return ResponseBean.error("回收站恢复失败");
        }else {
            return ResponseBean.success();
        }
    }

    @GetMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        int i = inStockService.delete(id);
        if (i == 0){
            return ResponseBean.error("回收站删除失败");
        }else {
            return ResponseBean.success();
        }
    }

    @PostMapping("/addIntoStock")
    public ResponseBean addIntoStock(@RequestBody InStockVO inStockVO){
        int i = inStockService.addIntoStock(inStockVO);
        if (i == 0){
            return ResponseBean.error("添加失败");
        }else {
            return ResponseBean.success();
        }
    }

    @PutMapping("/publish/{id}")
    public ResponseBean publish(@PathVariable Long id){
        int i = inStockService.publish(id);
        if (i == 0){
            return ResponseBean.error("审核失败");
        }else {
            return ResponseBean.success();
        }
    }
}
